package com.example.diyview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;
import androidx.annotation.Nullable;

@SuppressLint("AppCompatCustomView")

public class colorText extends TextView {
    private String rnm;
    private Paint blackPaint;
    private Paint redPaint;
    float offset = 0.5F;
    private int now;
    private boolean redOrBlack = true;
    synchronized void setNow(int now){
        this.now = now;
        invalidate();
    }
    public colorText(Context context) {
        this(context,null);
    }

    public colorText(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public colorText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        rnm = getText().toString();
        initPaint();
    }

    private void initPaint() {
        blackPaint = getPaintByColor(Color.BLACK);
        redPaint = getPaintByColor(Color.RED);
    }

    private Paint getPaintByColor(int color)
    {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(getTextSize());
        paint.setColor(color);
        return paint;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        float start = (now*1.0F/100*1.0F)*getWidth();
        int width = (int) (getWidth()*offset);
        int hight = getHeight();
        float end = (start + (getWidth()*offset));
        if(redOrBlack) drawBlack(canvas);
        else drawRed(canvas);
        if( end >= getWidth()*0.95)
        {
            if(redOrBlack==true)redOrBlack=false;
            else redOrBlack=true;
        }

    }
    private void drawBlack(Canvas canvas){
        float start = (now*1.0F/100*1.0F)*getWidth();
        int width = (int) (getWidth()*offset);
        int hight = getHeight();
        float end = (start + (getWidth()*offset));
        canvas.save();
        canvas.clipRect(start,0,end,hight);
        System.out.println(rnm);
        System.out.println(start);
        System.out.println(end);
        System.out.println(now);
        System.out.println((now*1.0F/100*1.0F));
        canvas.drawText(rnm,0,getHeight()*0.9F,redPaint);
        canvas.restore();
        canvas.save();
        canvas.clipRect(end,0,getWidth(),getHeight());
        canvas.drawText(rnm,0,getHeight()*0.9F,blackPaint);
        canvas.restore();
        canvas.save();
        canvas.clipRect(0,0,start,hight);
        System.out.println(rnm);
        canvas.drawText(rnm,0,getHeight()*0.9F,blackPaint);
        canvas.restore();
    }
    private void drawRed(Canvas canvas){
        float start = (now*1.0F/100*1.0F)*getWidth();
        int width = (int) (getWidth()*offset);
        int hight = getHeight();
        float end = (start + (getWidth()*offset));
        canvas.save();
        canvas.clipRect(start,0,end,hight);
        System.out.println(rnm);
        System.out.println(start);
        System.out.println(end);
        System.out.println(now);
        System.out.println((now*1.0F/100*1.0F));
        canvas.drawText(rnm,0,getHeight()*0.9F,blackPaint);
        canvas.restore();
        canvas.save();
        canvas.clipRect(end,0,getWidth(),getHeight());
        canvas.drawText(rnm,0,getHeight()*0.9F,redPaint);
        canvas.restore();
        canvas.save();
        canvas.clipRect(0,0,start,hight);
        System.out.println(rnm);
        canvas.drawText(rnm,0,getHeight()*0.9F,redPaint);
        canvas.restore();
    }
}
